from rest_framework.viewsets import ModelViewSet
from meiduo_mall_admin.my_paginate import MyPageNumberPagination
from goods.models import SKU
from rest_framework.generics import ListAPIView
from . import sku_serializers
from goods.models import GoodsCategory, SPU, SPUSpecification


# sku视图
class SKUViewSet(ModelViewSet):
    # 分页
    pagination_class = MyPageNumberPagination
    # 通用的序列化器
    serializer_class = sku_serializers.SKUViewSetserializer

    def get_queryset(self):
        keyword = self.request.query_params.get("keyword")
        if keyword:
            return SKU.objects.filter(name__contains=keyword).all()
        else:
            return SKU.objects.all()


# 获取三级信息分类
class SKUCategoryView(ListAPIView):
    # 序列化器
    serializer_class = sku_serializers.GoodsCategorySerializer
    # 数据源
    queryset = GoodsCategory.objects.all()


# sku的简要信息
class SKUSimpleView(ListAPIView):
    # 序列化器
    serializer_class = sku_serializers.SPUSerializer
    # 数据源
    queryset = SPU.objects.all()


# sku 商品的规格信息
class SKUGoodsSpecsView(ListAPIView):
    # 通用的序列化器
    serializer_class = sku_serializers.SPUSpecificationSerializer

    # 重写get_queryset
    def get_queryset(self):
        # 获取参数
        spu_id = self.kwargs.get("spu_id")
        # 返回响应
        return SPUSpecification.objects.filter(spu_id=spu_id).all()







